<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category RoleRepository.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/6  18:00
 * +--------------------------------------------------------+
 */

namespace App\Repositories;

use App\Models\Role;

class RoleRepository extends BaseRepository
{
    protected $role;

    public function __construct(Role $role)
    {
        $this->role = $role;
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  14:20
     */
    public function getInfo($id)
    {
        return $data = $this->role->find($id);
    }

    /**
     * @param $name
     * @param $guard_name
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  15:01
     */
    public function getInfoByName($name, $guard_name)
    {
        return $data = $this->role->query()->where('name', $name)->where('guard_name', $guard_name)->first();
    }

    /**
     * @param $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  13:39
     */
    public function getList($params)
    {
        $query = $this->role->query();

        $perPage = $this->perPage($params);
        [$sort, $order] = $this->paramsSet($params);

        $query = $query->where('guard_name', $params['guard_name']);

        if (!empty($params['name'])) {
            $query = $query->where('name', 'like', '%' . $params['name'] . '%');
        }
        if (!empty($params['start_at'])) {
            $query = $query->where('created_at', [$params['start_at'], $params['end_at']]);
        }

        return $list = $query->select('id', 'name', 'guard_name', 'created_at', 'updated_at')
            ->orderBy($sort, $order)
            ->paginate($perPage);
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  14:40
     */
    public function getAllList($params)
    {
        $query = $this->role->query();

        $query = $query->where('guard_name', $params['guard_name']);

        return $list = $query->get();
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  15:15
     */
    public function create($params)
    {
        return $this->role->query()->create($params);

    }

    /**
     * @param $role
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  16:24
     */
    public function update($role, $params)
    {
        return $role->update($params);
    }

    /**
     * @param $role
     * @return mixed
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/7  16:24
     */
    public function delete($role)
    {
        return $role->delete();
    }

    public function syncPermissions()
    {

    }

}
